package com.j4it.migration.modules.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.j4it.migration.common.persistence.Page;
import com.j4it.migration.common.service.CrudService;
import com.j4it.migration.modules.cms.dao.TableStateDao;
import com.j4it.migration.modules.cms.entity.TableState;

/**
 * 
 * @author zhengmingfu
 * @date 2016年3月25日
 *
 */
@Service
@Transactional(readOnly = true)
public class TableStateService extends CrudService<TableStateDao, TableState> {

    public Page<TableState> findPage(Page<TableState> page, TableState tableState) {
        tableState.setPage(page);
        page.setList(dao.findList(tableState));
        return page;
    }

    public List<List<TableState>> findListInterval(TableState tableState) {
        List<List<TableState>> list = new ArrayList<List<TableState>>();
        List<TableState> resource = dao.findListInterval(tableState);
        list.add(resource);// 加入原始数据
        List<TableState> increment = new ArrayList<TableState>();
        for (int i = 0; i < resource.size(); i++) {
            // 从数据库查出来的就是按日期升序，最后一天即目的日期是不需要查增量的
            if (i == resource.size() - 1) {
                continue;
            }
            TableState ts = new TableState();
            ts.setCreateTime(resource.get(i).getCreateTime());
            ts.setTableRows(resource.get(i + 1).getTableRows() - resource.get(i).getTableRows());
            increment.add(ts);
        }
        list.add(increment);// 加入增量数据
        return list;
    }
}
